package 滑动窗口.长度最小的子数组;

import utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Solution {
    public static int minSubArrayLen(int target, int[] nums) {
        int res = Integer.MAX_VALUE;
        int start = 0;
        int end = 0;
        int sum = 0;
        while(end < nums.length){
            sum += nums[end];
            // 需要多次修剪，可能新加的数字等于好几个元素的和
            while(sum >= target){
                res = Math.min(res,end - start +1);
                sum -= nums[start];
                start++;
            }
            end ++;
        }

        return res == Integer.MAX_VALUE ? 0 : res;
    }

    public static void main(String[] args) {
        int target = 7;
        int[] nums = {2,3,1,2,4,3};
        Utils.printInt(minSubArrayLen(target,nums));
    }
}
